package com.ngsky.ana.index;

import com.ngsky.ana.utils.ImageUtil;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.AncestorListener;
import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;

/**
 * @Author syx
 * @Desc
 * @Create 2020/6/14 14:47
 */
public class ImageItem extends JLabel {

    private final static int IMG_MAX_W = 300;
    private final static int IMG_MAX_H = 300;

    public ImageItem(String filePath) {
        try {
            setText(ImageUtil.getFileName(filePath));

            BufferedImage buff = ImageIO.read(new FileInputStream(filePath));
            buff = ImageUtil.thumbnail(buff, IMG_MAX_W, IMG_MAX_H);
            setIcon(new ImageIcon(buff));
            setHorizontalTextPosition(SwingConstants.CENTER);   // 水平方向文本在图片中心
            setVerticalTextPosition(SwingConstants.BOTTOM);     // 垂直方向文本在图片下方

            setPreferredSize(new Dimension(IMG_MAX_W, IMG_MAX_H));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
